import java.util.Stack;

public class Sort {
    public static void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else{
                    array[j+1] =tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    public static void ShellSort(int[] array){
        int gap = array.length;
        while(gap > 1){
            gap /= 2;
            Shell(gap,array);
        }
    }

    public static void Shell(int gap, int[] array){
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >= 0; j--) {
                if(array[j] > tmp){
                    array[j+gap] = array[j];
                }else{
                    array[j+gap] =tmp;
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    public static void SelectSort1(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            int minIndex = i;
            int j = i+1;
            for (; j < array.length; j++) {
                if(array[j] < array[minIndex]){
                   minIndex = j;
                }
            }
            swap(array, j, minIndex);
        }
    }
    private static void swap(int[]array,int j,int minIndex){
        int tmp = array[j];
        array[j] = array[minIndex];
        array[minIndex] = tmp;
    }

    public static void SelectSort(int[] array){
        int left = 0;
        int right = array.length-1;
        while(left < right){
            int minIndex = left;
            int maxIndex = left;
            for (int j = left+1; j <= right; j++) {
                if(array[j] < array[minIndex]){
                    minIndex = j;
                }
                if(array[j] > array[maxIndex]) {
                    maxIndex = j;
                }
            }
            swap(array, left, minIndex);
            if(maxIndex == left){
                maxIndex = minIndex;
            }
            swap(array, right, maxIndex);
            left++;
            right--;
        }
    }

    public static void quickSort(int[] array){
        int left = 0;
        int right = array.length-1;
        quick(array, left, right);
    }

    private static void quick(int[] array, int left, int right){
        if(left > right){
            return;
        }
        int par = position(array, left, right);

        quick(array, left, par-1);
        quick(array, par+1, right);
    }

    public static int position1(int[] array, int left, int right){
        int pivot = array[left];
        int i = left;
        while(left < right){
            while(left < right && array[right] >= pivot){
                right--;
            }
            while(left < right && array[left] <= pivot){
                left++;
            }
            swap(array, left, right);
        }
        swap(array, left, i);


        return left;
    }

    public static int position(int[] array, int left, int right){
        int pivot = array[left];

        while(left < right){
            while(left < right && array[right] >= pivot){
                right--;
            }
            array[left] = array[right];
            while(left < right && array[left] <= pivot){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = pivot;
        return left;

    }

    /**
     * 三数取中
     * @param array
     * @param left
     * @param right
     * @return
     */
    private static int findMid(int[] array, int left, int right){
        int mid = (left + right) / 2;
        if(array[left] < array[right]){
            if(array[right] < array[mid]){
                return array[right];
            }else if(array[left] < array[mid]){
                return array[mid];
            }else{
                return array[left];
            }
        }else{
            if(array[left] < array[mid]){
                return array[left];
            }else if(array[right] < array[mid]){
                return array[mid];
            }else{
                return array[right];
            }
        }

    }

    /**
     * 归并排序
     * @param array
     */
    public static void margeSort(int[] array){
        margeSortFunc(array, 0, array.length-1);

    }

    //递归
    public static void margeSortFunc(int[] array, int left, int right){
        //分解序lie
        int mid = (left + right) / 2;
        if(left == right){
            return;
        }
        margeSortFunc(array, left, mid);
        margeSortFunc(array, mid+1, right);

        //合并序列
        marge(array, left, mid, right);

    }

    //非递归


    //合并两个有序序列
    public static void marge(int[] array, int left, int mid, int right){
        int start1 = left;
        int end1 = mid;
        int start2 = mid + 1;
        int end2 = right;
        int[] tmp = new int[right - left + 1];
        int k = 0;
        while(start1 <= end1 && start2 <= end2){
            if(array[start1] < array[start2]){
                tmp[k++] = array[start1++];
            }else{
                tmp[k++] = array[start2++];
            }
        }

        while(start1 <= end1){
            tmp[k++] = array[start1++];
        }

        while(start2 <= end2){
            tmp[k++] = array[start2++];
        }

        for (int i = 0; i < tmp.length; i++) {
            array[i + left] = tmp[i];
        }
    }

    //堆排序
    public static void heapSort(int[] array){
        int useSize = array.length;
        createHeap(array,useSize);
        int end = useSize - 1;
        while(end > 0){
            swap(array, 0, end);
            siftDown(array,0,end);
            end--;
        }
    }

    private static void createHeap(int[] array, int useSize){
        for (int parent = (useSize-1-1)/2; parent>=0; parent--) {
            siftDown(array, parent, useSize);
        }
    }

    private static void siftDown(int[] array, int parent, int len){
        int child = 2*parent + 1;
        while(child < len){
            if(child+1 < len && array[child] < array[child+1]){
                child++;
            }
            if(array[parent] < array[child]){
                swap(array, parent, child);
                parent = child;
                child = 2*parent + 1;
            }else{
                break;
            }
        }
    }

    //冒泡排序
    public static void bbruleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] < array[j+1]){
                    swap(array, j,j+1);
                    flg = true;
                }
            }
            if(!flg){
                break;
            }
        }
    }

    //非递归快速排序
    public static void quickSort2(int[] array){
        int left = 0;
        int right = array.length-1;
        Stack<Integer> stack = new Stack<>();
        stack.push(left);
        stack.push(right);
        int par = position(array, left, right);
        if(par > left + 1){
            stack.push(left);
            stack.push(par-1);
        }
        if(par < right - 1){
            stack.push(par+1);
            stack.push(right);
        }
        while(!stack.empty()){
            right = stack.pop();
            left = stack.pop();
            par = position(array, left, right);
            if(par > left + 1){
                stack.push(left);
                stack.push(par-1);
            }
            if(par < right - 1){
                stack.push(par+1);
                stack.push(right);
            }
        }
    }

    //非递归归并排序

    public static void margeSort2(int[] array){
        margeSortFunc2(array, 0, array.length-1);

    }
    public static void margeSortFunc2(int[] array, int left, int right){
        //分解序lie

        int len = array.length-1;
        int gap = 1;
        while(gap < len){
            for (int i = 0; i <= len; i += 2*gap) {
                left = i;
                int mid = left + gap - 1;
                right = mid + gap;
                if(mid > len){
                    mid = len;
                }
                if(right > len){
                    right = len;
                }
                marge(array,left,mid,right);
            }
            gap *= 2;
        }
    }

}
